package com.pawn.jwt.controller;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureGenerationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pawn.jwt.entity.SysUser;
import com.pawn.jwt.service.SysUserService;
import com.pawn.jwt.utils.JWTUtil;
import com.pawn.jwt.utils.ResUtil;
import com.pawn.jwt.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: springboot-1-cache
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-08-13
 **/
@RestController
@Slf4j
public class SysUserController {
    @Autowired
    SysUserService sysUserService;

    /**
     * 注册
     *
     * @param user
     * @return
     */
    @PostMapping("/reg")
    public ResUtil addUser(@RequestBody SysUser user) {
        boolean save = sysUserService.save(user);
        ResUtil resUtil = ResUtil.SUCCESS("注册成功", user);
        return resUtil;
    }

    /**
     * 用户登录，返回token
     *
     * @param user
     * @return
     */
    @PostMapping("/login")
    public ResUtil login(@RequestBody SysUser user) {
        log.info("用户名[{}]", user.getName());
        log.info("密码[{}]", user.getPwd());
        Map<String, Object> map = new HashMap<>();
        SysUser one = sysUserService.getOne(new QueryWrapper<>(user));
        if (one != null) {
            Map<String, String> userMap = new HashMap<>();
            userMap.put("name", user.getName());
            userMap.put("pwd", user.getPwd());
            String token = JWTUtil.getToken(userMap);
            return ResUtil.SUCCESS("登录成功", token);
        } else {
            return ResUtil.ERROR("登录失败", "");
        }
    }

    /**
     * 解析token
     *
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/login")
    public ResUtil getLoginUser(HttpServletRequest request, HttpServletResponse response) {
        String token = request.getHeader("token");
        DecodedJWT verify = JWTUtil.verify(token);
        String name = verify.getClaim("name").asString();
        String format = TimeUtil.format(verify.getExpiresAt());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", name);
        jsonObject.put("time", format);
        return ResUtil.SUCCESS("请求成功", jsonObject);
    }

    /**
     * 根据Id获取用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/user")
    public ResUtil getUser(@RequestParam("id") Long id) {
        SysUser byId = sysUserService.getById(id);
        return ResUtil.SUCCESS("获取成功", byId);
    }

    /**
     * 获取所有的用户
     *
     * @return
     */
    @GetMapping("/users")
    public ResUtil getUsers() {
        List<SysUser> list = sysUserService.list();
        return ResUtil.SUCCESS("获取成功", list);
    }

}
